package com.zsrt.java.ex.reflect.reflect01;

import static org.junit.Assert.*;

import java.lang.reflect.Constructor;

import org.junit.Test;

/**
 * getClss得到的是Person字解码文件
 * 总结：：
 * 1一般开发使用forName();
 * 2.因为第三个是一个字符串，不是一个类名 所以可以写在配置文件中
 * 
 * @author My
 *
 */
public class ReflectTest {
	
	@Test
	public void testName() throws Exception {
		
		Person p1=new Person();
		Class clazz=p1.getClass();
		
		Person p2=new Person();
		Class clazz2 = p2.getClass();
		System.out.println(p1==p2);
		System.out.println(clazz==clazz2);
		
		
		Class clazz3=Person.class;
		System.out.println(clazz==clazz3);
		
		Class clazz4=Class.forName("com.zsrt.java.ex.reflect.reflect01.Person");
		System.out.println(clazz==clazz4);
		
	}
	
	/**
	 * getconstructors无法获取私有的方法
	 * getDeclaredConstructors 能获取所有方法
	 * @throws Exception
	 */
	@Test
	public void test2() throws Exception {
		Class clazz=Class.forName("com.zsrt.java.ex.reflect.reflect01.Person");
		Constructor[] constructor=clazz.getConstructors();
		for(Constructor construc: constructor){
			System.out.println(construc);
		}
		
		System.out.println("==================================");
		Constructor[] constructor2=clazz.getDeclaredConstructors();
		for (Constructor con : constructor2) {
			System.out.println(con);
			
		}
		
		System.out.println("==================================");
		Constructor declaredConstructor=clazz.getDeclaredConstructor();
		Object object=declaredConstructor.newInstance();
		//测试object是否是Person类的实例
		System.out.println(object instanceof Person);
		
		System.out.println("==================================");
		
		Constructor declaredConstructor2=clazz.getDeclaredConstructor(int.class,String.class );
		Object object2=declaredConstructor2.newInstance(110,"狗娃");	
		System.out.println(object2);
		
		System.out.println("==================================");
		
		Constructor declaredConstructor3=clazz.getDeclaredConstructor(String.class);
		
		
		declaredConstructor3.setAccessible(true);
		Object ob = declaredConstructor3.newInstance(110);
		
		System.out.println(ob);
		
		
		
		
		
		
		
		
	}
	
	
}
